﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BirdController : MonoBehaviour {

    public float Speed =5;
    public float JumpSpeed =2;

    private Rigidbody2D body;

    public void GameStart()
    {
        this.body.gravityScale = 1;
    }

    public void Jump()
    {
        Vector2 velocity = this.body.velocity;

        if (velocity.y < 0)
        {
            velocity.y = 0;
        }
        velocity.y = velocity.y + this.JumpSpeed * this.JumpSpeed / (this.JumpSpeed + velocity.y);
        this.body.velocity = velocity;
    }

    public void Prepare()
    {
        this.body = this.GetComponent<Rigidbody2D>();
        this.body.velocity = new Vector2(Speed, 0);
        // UnityEngine.SceneManagement.SceneManager.GetActiveScene()
        this.body.gravityScale = 0;
    }

    // Use this for initialization
    void Start () {
        this.body = this.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update () {
        Vector2 velocity = this.body.velocity;
        if (velocity.x != 0 || velocity.y != 0)
        {
            //this.transform.localRotation = Quaternion.Euler(0, 0, Mathf.Atan(velocity.y / velocity.x) / 3.14f * 180);
            this.transform.localRotation = Quaternion.FromToRotation(Vector3.right, velocity);
            //this.transform.localRotation = Quaternion.LookRotation();
            // this.transform.forward = new Vector3(velocity.x, velocity.y, 0);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("OnTriggerEnter");
    }

    private void OnCollisionEnter(Collision collision)
    {
        Debug.Log("OnCollisionEnter");
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("OnCollisionEnter2D");
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.name== "ShowLand")
        {
            GameMgr.Instance().GameOver();
            this.body.gravityScale = 0;
            this.body.velocity = new Vector2(0, 0);
        }
        if (collision.gameObject.tag == "Tude")
        {
            GameMgr.Instance().GameOver();
            this.body.velocity = new Vector2(0, 0);
        }
    }
}
